package com.lujieni.springbootflowable;

import org.flowable.engine.ProcessEngine;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 流程变量
 */
@SpringBootTest
class SpringbootFlowableApplicationTests2 {

    @Autowired
    private ProcessEngine processEngine;
    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private TaskService taskService;

    /**
     *
     * 流程部署
     * act_re_procdef 流程定义实例
     * act_re_deployment 流程定义部署
     *
     */
    @Test
    void deploy() {
        RepositoryService repositoryService = processEngine.getRepositoryService();
        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource("process/first_flow.bpmn20.xml")
                .name("firstflow")
                .deploy();
        System.out.println(deployment.getId());
    }

    /**
     * 启动流程实例并设置全局参数
     * act_ru_variable表中会插入数据
     */
    @Test
    void startProcessWithVariables() {
        Map<String, Object> variables = new HashMap<>();
        variables.put("name", "lujieni");
        variables.put("age", "30");
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("firstflow", variables);
    }

    /**
     * 流程实例启动后也可以设置全局参数
     * processInstanceId 是 ExecutionId的父级
     */
    @Test
    public void setVariables(){
        String executionId = "b2d1ec35-8676-11f0-b9a9-5847ca7eb09c";
        // 流程实例全局绑定,并行分支之间也能共享,存储在 ACT_RU_VARIABLE 表中,EXECUTION_ID_ 指向流程实例
        runtimeService.setVariable(executionId, "like", "eat");
        // 如果在并行分支中,ACT_RU_VARIABLE 表中的EXECUTION_ID_和PROC_INST_ID_不一致
        runtimeService.setVariableLocal(executionId, "weight", "160");
        // 通过task设置,也是全局的
        taskService.setVariable("b2d4ab5c-8676-11f0-b9a9-5847ca7eb09c", "height", "172");
        // 只在对应taskId上有效,别的节点都看不到
        taskService.setVariableLocal("b2d4ab5c-8676-11f0-b9a9-5847ca7eb09c", "lover", "qizi");

    }

    /**
     * 完成任务的时候也可以传入全局变量
     */
    @Test
    public void completeTask(){
        TaskService taskService = processEngine.getTaskService();
        Map<String, Object> map = new HashMap<>();
        map.put("lover", "qizi");
        // 也是全局的
        taskService.complete("b2d4ab5c-8676-11f0-b9a9-5847ca7eb09c", map);
    }

    /**
     * task维度获取全局变量,通过taskId即可
     */
    @Test
   public void getVariables(){
       TaskService taskService = processEngine.getTaskService();
       Map<String, Object> variables = taskService.getVariables("b2d4ab5c-8676-11f0-b9a9-5847ca7eb09c");
       System.out.println(variables);
   }

}
